<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitealbum
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: AlbumController.php 2011-08-026 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */

class Sitealbum_AlbumController extends Core_Controller_Action_Standard {

  public function init() {
    if (!$this->_helper->requireAuth()->setAuthParams('album', null, 'view')->isValid())
      return;

    $hidePhotoUrl = Engine_Api::_()->getApi('settings', 'core')->getSetting('sitealbum.hide.photourl', null);
    if (empty($hidePhotoUrl)) {
      return;
    }

    if (0 !== ($album_id = (int) $this->_getParam('album_id')) &&
            null !== ($album = Engine_Api::_()->getItem('album', $album_id))) {
      Engine_Api::_()->core()->setSubject($album);
    }
  }

  public function viewAction() {
    $settings = Engine_Api::_()->getApi('settings', 'core');

    if (!$this->_helper->requireSubject('album')->isValid())
      return;

    $this->view->album = $album = Engine_Api::_()->core()->getSubject();

    if (!$this->_helper->requireAuth()->setAuthParams($album, null, 'view')->isValid())
      return;

		$sitealbumType = Engine_Api::_()->getApi('settings', 'core')->getSetting('sitealbum.viewertype', null);
		if( empty($sitealbumType) ){
			return;
		}

		$getLightBox = Zend_Registry::isRegistered('sitealbum_getlightbox') ? Zend_Registry::get('sitealbum_getlightbox') : null;
		if( empty($getLightBox) ){ return; }

    $this->_helper->content
            ->setNoRender()
            ->setEnabled()
    ;
    //START: "SUGGEST TO FRIENDS" LINK WORK
    $is_suggestion_enabled = Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled('suggestion');
    //HERE WE ARE DELETE THIS ALBUM SUGGESTION IF VIEWER HAVE
    if (!empty($is_suggestion_enabled)) {
			Engine_Api::_()->sitealbum()->deleteSuggestion('album', $album->getIdentity(), 'album_suggestion');
    }
  }

  public function featuredAction() {
    if (!$this->_helper->requireSubject('album')->isValid())
      return;

    $this->view->album = $album = Engine_Api::_()->core()->getSubject();
    $album->featured = !$album->featured;
    $album->save();
    exit(0);
  }

  //ACTION FOR ADDING ALBUM OF THE DAY
  public function addAlbumOfDayAction() {
    //FORM GENERATION
    $album = Engine_Api::_()->core()->getSubject();
    $form = $this->view->form = new Sitealbum_Form_ItemOfDayday();
    $form->setTitle('Make this Album of the Day')
            ->setDescription('Select a start date and end date below.This album will be displayed as "Album of the Day" for this duration.If more than one albums of the day are found for a date then randomly one will be displayed.');

    //CHECK POST
    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {

			$featuredAlbumType = Engine_Api::_()->getApi('settings', 'core')->getSetting('sitealbum.featuredalbum', null);
			if( empty($featuredAlbumType) ){
				return;
			}

      //GET FORM VALUES
      $values = $form->getValues();
      $values["resource_id"] = $album->getIdentity();
      //BEGIN TRANSACTION
      $db = Engine_Db_Table::getDefaultAdapter();
      $db->beginTransaction();
      try {

        $table = Engine_Api::_()->getDbtable('itemofthedays', 'sitealbum');
        $row = $table->getItem('album', $values["resource_id"]);
        if (empty($row)) {
          $row = $table->createRow();
        }
        $values = array_merge($values, array('resource_type' => 'album'));

        if ($values['start_date'] > $values['end_date'])
          $values['end_date'] = $values['start_date'];
        $row->setFromArray($values);
        $row->save();

        $db->commit();
      } catch (Exception $e) {
        $db->rollBack();
        throw $e;
      }
      return $this->_forward('success', 'utility', 'core', array(
          'messages' => array(Zend_Registry::get('Zend_Translate')->_('Your changes have been saved.')),
          'layout' => 'default-simple',
          'smoothboxClose' => true,
      ));
    }
  }

}